package com.tanhua.server.serivce;

import com.tanhua.dubbo.api.*;
import com.tanhua.model.db.*;
import com.tanhua.model.dto.OptionDto;
import com.tanhua.model.vo.*;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TestSoulService {

    @DubboReference
    private NairApi nairApi;

    @DubboReference
    private ReportApi reportApi;

    @DubboReference
    private NairTopicApi nairTopicApi;

    @DubboReference
    private TopicApi topicApi;

    @DubboReference
    private CountApi countApi;

    @DubboReference
    private ConclusionApi conclusionApi;

    @DubboReference
    private DimensionsApi dimensionsApi;

    @DubboReference
    private DimensionsReportApi dimensionsReportApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    public List<NairVo> testSoul() {
        Long userId = UserHolder.getUserId();
        //查询所有问卷
        List<Nair> nairList = nairApi.findAll();  //先循环这个啊
        //查询当前用户报告信息
        List<Report> reportList = reportApi.findByUserId(userId);
        //用于返回页面的vo实体类对象
        List<NairVo> vos = new ArrayList<>();
        //查询做过的报告，序号order最大的序号时多少
        Integer bigOrder = 0;
        for (Report report : reportList) {
            Integer nairId = report.getNairId();
            List<Integer> sortList = nairApi.findSortById(nairId);
            for (Integer integer : sortList) {
                if (integer > bigOrder){
                    bigOrder = integer;
                }
            }
        }
        for (Nair nair : nairList) {
            Integer nair_id = nair.getNairId();
            NairVo vo = NairVo.init(nair);
            if (nair.getLevel().equals("初级")){
                vo.setIsLock(0);
            }
            for (Report report : reportList) {
                Integer nairId = report.getNairId();
                if (nair_id == nairId){
                    vo.setReportId(report.getReportId().toString());
                }
            }
            //判断有没有做过，设置报告id
            //序号小于order的，全部设置已解锁
            if (nair.getSort() <= bigOrder+1){
                vo.setIsLock(0);
            }else {
                vo.setIsLock(1);
            }
            List<TopicVo> questions = new ArrayList<>();
            //查询当前问卷中所有的问题ID
            List<Long> questionsIds = nairTopicApi.findByNairId(nair_id);
            //用所有的问卷ID去查询所有的题目信息
            List<Topic> topics= topicApi.findByIds(questionsIds);
                for (Topic topic : topics) {
                    TopicVo topicVo = TopicVo.init(topic);
                    //用于接受Topic中options属性的集合
                    List<OptionVo> options = new ArrayList<>();
                    Integer questionId = topic.getQuestionId();
                    //按照当亲试题ID找到所属的选项集合
                    List<Count> countList = countApi.findByQuestionId(questionId);
                    for (Count count : countList) {
                        OptionVo optionVo = OptionVo.init(count);
                        options.add(optionVo);
                    }
                    topicVo.setOptions(options);
                    questions.add(topicVo);
                }
            vo.setQuestions(questions);
            vos.add(vo);
        }

        return vos;
    }

    public String sendResult(List<OptionDto> list) {
        Long userId = UserHolder.getUserId();
//        List<Report> byUserId = reportApi.findByUserId(userId);
        Report report = new Report();
        Integer score = 0;
        OptionDto nairIdFind = list.get(0);
        String questionId = nairIdFind.getQuestionId();
        Integer nairId = nairTopicApi.findIdByTopic(questionId);
        for (OptionDto optionDto : list) {
            score +=countApi.findScoreById(optionDto);
        }
        Integer conclusionId = conclusionApi.findIdByScore(score);
        //Integer dimensionsId = dimensionsApi.findIdByScore(score);
        report.setScore(Long.valueOf(score));
        report.setNairId(nairId);
        report.setConclusionId(conclusionId);
        report.setUserId(userId);
        Integer reportId = reportApi.save(report);
        List<Dimensions> dimensions = dimensionsApi.findAll();
        for (Dimensions dimension : dimensions) {
            DimensionsReport dimensionsReport = new DimensionsReport();
            dimensionsReport.setDimensionsId(dimension.getDimensionsId());
            dimensionsReport.setReportId(reportId);
            if (dimension.getDimensionsId() == 1){
                dimensionsReport.setDimensionsScore(score+5+"%");
            }else if (dimension.getDimensionsId() == 2){
                dimensionsReport.setDimensionsScore(score+10+"%");
            }else if (dimension.getDimensionsId() == 3){
                dimensionsReport.setDimensionsScore(score-5+"%");
            }else if (dimension.getDimensionsId() == 4){
                dimensionsReport.setDimensionsScore(score-15+"%");
            }
            dimensionsReportApi.save(dimensionsReport);
        }
        return reportId.toString();
    }

    public ReportVo report(Integer id) {
        ReportVo vo = new ReportVo();
        Report report = reportApi.findById(id);
        Conclusion conclusion = conclusionApi.findById(report.getConclusionId());
        vo.setConclusion(conclusion.getConclusionContext());
        vo.setCover(conclusion.getConclusionCover());
        List<DimensionsVo> dimensionsList = new ArrayList<>();
        List<DimensionsReport> dimensionsReports = dimensionsReportApi.findDimensionByReport(id);
        for (DimensionsReport dimensionsReport : dimensionsReports) {
            DimensionsVo dimensionsVo = new DimensionsVo();
            String dimensionsContext = dimensionsApi.findById(dimensionsReport.getDimensionsId());
            dimensionsVo.setKey(dimensionsContext);
            dimensionsVo.setValue(dimensionsReport.getDimensionsScore());
            dimensionsList.add(dimensionsVo);
        }
        vo.setDimensions(dimensionsList);
        Long score = report.getScore();
        List<SimilarYouVo> similarYouVos = new ArrayList<>();
        List<Report> reportList = reportApi.findAll();
        for (Report report1 : reportList) {
            if(report1.getUserId() != UserHolder.getUserId()){
                SimilarYouVo similarYouVo = new SimilarYouVo();
                Long scoreAll = report1.getScore();
                if (score > scoreAll){
                    if (score - scoreAll <10){
                        Long userId = report1.getUserId();
                        UserInfo userInfo = userInfoApi.findById(userId);
                        similarYouVo.setId(Integer.valueOf(userInfo.getId().toString()));
                        similarYouVo.setAvatar(userInfo.getAvatar());
                        similarYouVos.add(similarYouVo);
                    }
                }else if (score < scoreAll) {
                    if (scoreAll - score <10){
                        Long userId = report1.getUserId();
                        UserInfo userInfo = userInfoApi.findById(userId);
                        similarYouVo.setId(Integer.valueOf(userInfo.getId().toString()));
                        similarYouVo.setAvatar(userInfo.getAvatar());
                        similarYouVos.add(similarYouVo);
                    }
                }else {
                    Long userId = report1.getUserId();
                    UserInfo userInfo = userInfoApi.findById(userId);
                    similarYouVo.setId(Integer.valueOf(userInfo.getId().toString()));
                    similarYouVo.setAvatar(userInfo.getAvatar());
                    similarYouVos.add(similarYouVo);
                }
                vo.setSimilarYou(similarYouVos);
            }
        }
        return vo;
    }
}
